/**
 * Copyright © 2008, EMC Corporation.
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 *     + Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     + Redistributions in binary form must reproduce the above copyright 
 *       notice, this list of conditions and the following disclaimer in the 
 *       documentation and/or other materials provided with the distribution.
 *     + The name of EMC Corporation may not be used to endorse or promote 
 *       products derived from this software without specific prior written 
 *       permission.
 *
 *      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 *      "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
 *      TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 *      PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
 *      BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 *      CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 *      SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 *      INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *      CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 *      ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 *      POSSIBILITY OF SUCH DAMAGE.
 */
package com.emc.esu.api {
	/**
	 * An Access Control List (ACL) is a collection of Grants that assign privileges
	 * to users and/or groups.
	 */
	public class Acl {
	    private var grants:Array;
	
	    /**
	     * Creates a new access control list
	     */
	    public function Acl() {
	        this.grants = new Array();
	    }
	    
	    /**
	     * Gets the grant array
	     * @return this ACL's grants
	     */
	    public function getGrants():Array {
	    	return grants;
	    }
	    
	    /**
	     * Adds a grant to the access control list
	     * @param g the grant to add.
	     */
	    public function addGrant( g:Grant ):void {
	        grants.push( g );
	    }
	    
	    /**
	     * Removes a grant from the access control list.
	     * @param g the grant to remove
	     */
	    public function removeGrant( g:Grant ):void {
	    	var a:Array = new Array();
	    	for each( var gr:Grant in grants ) {
	    		if( !g.equals( gr ) ) {
	    			a.push( gr );
	    		}
	    	}
	        grants = a;
	    }
	    
	    public function count():int {
	        return grants.length;
	    }
	    
	    /**
	     * Clears all the grants in the ACL.
	     */
	    public function clear():void {
	        grants = new Array();
	    }
	    
	    /**
	     * Returns true if the ACLs are equal.  This is done by ensuring they
	     * have the same number of grants and each ACL contains the same
	     * set of grants.
	     */
	    public function equals( obj:Object ):Boolean {
	        if( !( obj is Acl ) ) {
	            return false;
	        }
	        
	        var acl2:Acl = Acl(obj);
	        
	        if( count() != acl2.count() ) {
	        	return false;
	        }
	        
	        for each( var g:Grant in grants ) {
	        	if( !acl2.contains( g ) ) {
	        		return false;
	        	}
	        }
	
	        return true;
	    }
	    
	    /**
	     * Returns the ACL's grant set as a String.
	     */
	    public function toString():String {
	        return grants.toString();
	    }
	
	    /**
	     * Returns true if this ACL contains the specified Grant.
	     * @param g the grant to check for
	     * @return true if this ACL contains the grant.
	     */
	    public function contains(g:Grant):Boolean {
	    	for each( var gr:Grant in grants ) {
	    		if( g.equals( gr ) ) {
	    			return true;
	    		}
	    	}
	    	return false;
	    }
	}
}